//
//      Copyright Timothy Evans 2014 2015    timothy.evans@gsecraif.org
//
//      This file is part of gsecraif.
//
//      gsecraif is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 3 of the License, or
//      (at your option) any later version.
//
// stripe
//
using namespace std;
#ifndef STRIPE_H
#define STRIPE_H

#include <assert.h>
#include <algorithm>
#include <vector>
#include <iterator>

#include "parity.h"

// A Stripe is a fixed length array of length bytes, including one parity byte
// for use with gsecraif
// The parity byte may appear anywhere in the array,
// because it is rotated before written to files
// An "un-rotated" stripe has the parity as the last byte.
// E.g. un-rotated: a b c d e P
// E.g. rotated: d e P a b c
class Stripe {
    int parity_position;  // position of parity byte in array (0 to length-1)
    vector<unsigned char> bytes; // bytes including parity byte
public:
    int position_of_parity();    // return parity_position;
    void increase_parity_pos();  // increase parity position
    void decrease_parity_pos();  // decrease parity position
    void rotate_stripe(int stripestart);    // rotate the stripe
    void de_rotate_stripe(int stripestart); // de-rotate the stripe
    void repair_stripe(int pos_bad_byte);   // repair stipe with "bad" byte
                                            // at pos_bad_byte
    // if a byte is "missing" it will arbitrarily be set to 0 until repaired

    // take slice and add parity to produce stripe:
    void slice_to_stripe(vector<unsigned char> slice);

    vector<unsigned char> slice_from_stripe(); // return slice from stripe
    unsigned char get_byte(int position);      // return byte at location -
                                               // position
    void put_byte(int position, unsigned char byte); // assign byte at location
                                                     // - position

// constructor

// stripe of len bytes
Stripe(int len)  {
    for (int compfileid=0; compfileid != len; ++compfileid) {
        bytes.push_back(0);
        }
    parity_position = len -1; // set parity position to last byte
    }

};  // end of class
 // ------------


#endif /* not STRIPE_H */
